import Vue from 'vue'
import axios from 'axios'
const getBaseUrl = () => {
  return Vue.prototype.VUE_APP_BASE_API
}

export const getDesignerUrl = () => {
  return Vue.prototype.VUE_APP_DESIGNER_URL
}

const service = axios.create({
  timeout: 20000 // request timeout
})
// 全局定义一个存放取消请求的标识
window.axiosCancel = []
service.interceptors.request.use(
  (config) => {
    if (!config.headers['Content-Type']) {
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    }
    config.baseURL = config.hasOwnProperty('baseURL') ? config.baseURL : getBaseUrl()
    return config
  },
  (error) => {
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

const handleError = (error) => {
  try {
    if (process.env.NODE_ENV === 'development') {
      let message = error.message || ''
      if (error.response && error.response.data && error.response.data.detailMsg) {
        message = error.response.data.detailMsg
      }
    } else {
    }
    return Promise.reject(error)
  } catch (error) {}
}

// response interceptor
service.interceptors.response.use(
  (response) => {
    const config = response.config
    const res = response.data
    if (config.responseType === 'blob') {
      return res
    }
    if (!res.data) {
    }
    // if the custom code is not 20000, it is judged as an error.
    if (res.ackCode && res.ackCode !== 200) {
      // 50008: Illegal token; 50012: Other clients logged in; 40000: Token expired;
      if (res.ackCode === 40000 || res.ackCode === 50012 || res.ackCode === 50008) {
        return null
      } else if (res.ackCode === 40005) {
        return null
      } else if (res.message) {
        if (res.data['records']) {
          res.data['data'] = res.data['records']
        }
        return res
      }

      return handleError(new Error('请联系管理员！'))
    } else {
      return res
    }
  },
  (error) => {
    if (error.response.data.config) {
      !error.response.data.config.url.includes('ElectricalCard/getCardFile') && Message.error(error.response.data.message)
    }
    return error.response.data
    // return error
  }
  // handleError
)

export default service

/**
 * The workhorse; converts an object to x-www-form-urlencoded serialization. for spare,now use qs.stringify
 * @param {Object} obj
 * @return {String}
 */

export function paramToUrlencoded(obj) {
  var query = ''
  var name
  var value
  var fullSubName
  var subName
  var subValue
  var innerObj
  var i

  for (name in obj) {
    value = obj[name]

    if (value instanceof Array) {
      for (i = 0; i < value.length; ++i) {
        subValue = value[i]
        fullSubName = name + '[' + i + ']'
        innerObj = {}
        innerObj[fullSubName] = subValue
        query += paramToUrlencoded(innerObj) + '&'
      }
    } else if (value instanceof Object) {
      for (subName in value) {
        subValue = value[subName]
        fullSubName = name + '[' + subName + ']'
        innerObj = {}
        innerObj[fullSubName] = subValue
        query += paramToUrlencoded(innerObj) + '&'
      }
    } else if (value !== undefined && value !== null) query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'
  }

  return query.length ? query.substr(0, query.length - 1) : query
}
